﻿using AutoMapper;
using MediatR;
using SmartTracking.API.Write.Application.Commands.SheepFarm_command;
using SmartTracking.Domain.Sheep;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;

namespace SmartTracking.API.Write.Application.CommandHandlers.SheepFarm_handler
{
    public class UpdateSheepFramCommandHandler:IRequestHandler<UpdateSheepFramCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<SheepFarm> sheepfarmRep;
        private readonly IMapper mapper;

        public UpdateSheepFramCommandHandler(
            IBaseRepository<SheepFarm> sheepfarmRep,
            IMapper mapper)
        {
            this.sheepfarmRep = sheepfarmRep;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(UpdateSheepFramCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.更新成功;
            result.Message = ApiEnum.更新成功.ToString();

            SheepFarm sheep = mapper.Map<SheepFarm>(request);
            sheep.UpdateTime = DateTime.Now;
            
            var list =await sheepfarmRep.UpdateAsync(sheep);
            result.data = list;
            return await Task.FromResult(result);
        }
    }
}
